import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;


public class SallyToHAH {

	public static void main(String[] args) throws IOException {
		HashMap<String, String> label = new HashMap<String, String>();
		label.put("doisong", "1");
		label.put("giaitri", "2");
		label.put("kinhdoanh", "3");
		label.put("sohoa", "4");
		label.put("thethao", "5");
		
		ArrayList<String> fileName = new ArrayList<String>();
		fileName.add("1_2");
//		fileName.add("1_3");
//		fileName.add("1_4");
//		fileName.add("1_5");
//		fileName.add("2_3");
//		fileName.add("2_4");
//		fileName.add("2_5");
//		fileName.add("3_4");
//		fileName.add("3_5");
		fileName.add("4_5");
		fileName.add("3_4_5");
		fileName.add("1_2_3_4_5");
		
		
		ArrayList<String> class1 = new ArrayList<String>();
		ArrayList<String> class2 = new ArrayList<String>();
		ArrayList<String> class3 = new ArrayList<String>();
		ArrayList<String> class4 = new ArrayList<String>();
		ArrayList<String> class5 = new ArrayList<String>();
		
		//read libsvm file and save to 5 array
		String fileDicName = "5k.libsvm";
		try {
			File fileDir = new File(fileDicName);

			BufferedReader in = new BufferedReader(new InputStreamReader(
					new FileInputStream(fileDir), "UTF8"));

			String str;			
			String docLabel;
			

			while ((str = in.readLine()) != null) {
				if (str.contains("#")) {
					docLabel = str.substring(str.indexOf("#"), str.length());
					if (label.containsKey(docLabel.split(" ")[1])) {
						String labelNumber = docLabel.split(" ")[1];
						
						str = label.get(labelNumber)
								+ str.substring(str.indexOf(" "),
										str.indexOf("#")) + "\n";
						
						if(labelNumber.equalsIgnoreCase("doisong")){							
							class1.add(str);
//							System.out.println(str);
						}
						if(labelNumber.equalsIgnoreCase("giaitri")){
							class2.add(str);
//							System.out.println(str);
						}
						if(labelNumber.equalsIgnoreCase("kinhdoanh")){
							class3.add(str);
//							System.out.println(str);
						}
						if(labelNumber.equalsIgnoreCase("sohoa")){
							class4.add(str);
//							System.out.println(str);
						}
						if(labelNumber.equalsIgnoreCase("thethao")){
							class5.add(str);
//							System.out.println(str);
						}
						System.out.println(str);
					}					
				}			
				
			}

			in.close();
		} catch (UnsupportedEncodingException e) {
			System.out.println(e.getMessage());
		} catch (IOException e) {
			System.out.println(e.getMessage());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
//		java.util.Collections.sort(dictionary);
		
		
		
		String[] fileNameArray;
		for (int i = 0; i < fileName.size(); i++) {
			fileNameArray = fileName.get(i).split("_");
			
			String outputFileName = "h2a." + fileName.get(i) + ".train";
			File fileDir = new File(outputFileName);
			BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(fileDir), "UTF8"));
			
			if(fileNameArray.length > 2){
				for(int j = 0; j < fileNameArray.length/2; j++){
					if(fileNameArray[j].equalsIgnoreCase("1")){
						for(int k = 0; k < class1.size(); k++){
							out.append(class1.get(k));					
						}	
					} else if(fileNameArray[j].equalsIgnoreCase("2")){
						for(int k = 0; k < class2.size(); k++){
							out.append("1 " + class2.get(k).substring(2, class2.get(k).length()));					
						}	
					} else if(fileNameArray[j].equalsIgnoreCase("3")){
						for(int k = 0; k < class3.size(); k++){
							out.append("1 " + class3.get(k).substring(2, class3.get(k).length()));						
						}
					} else if(fileNameArray[j].equalsIgnoreCase("4")){
						for(int k = 0; k < class4.size(); k++){
							out.append("1 " + class4.get(k).substring(2, class4.get(k).length()));						
						}	
					} else if(fileNameArray[j].equalsIgnoreCase("5")){
						for(int k = 0; k < class5.size(); k++){
							out.append("1 " + class5.get(k).substring(2, class5.get(k).length()));						
						}	
					}
				}
				for(int j = fileNameArray.length/2; j < fileNameArray.length; j++){
					if(fileNameArray[j].equalsIgnoreCase("1")){
						for(int k = 0; k < class1.size(); k++){
							out.append(class1.get(k));					
						}	
					} else if(fileNameArray[j].equalsIgnoreCase("2")){
						for(int k = 0; k < class2.size(); k++){
							out.append("2 " + class2.get(k).substring(2, class2.get(k).length()));					
						}	
					} else if(fileNameArray[j].equalsIgnoreCase("3")){
						for(int k = 0; k < class3.size(); k++){
							out.append("2 " + class3.get(k).substring(2, class3.get(k).length()));						
						}
					} else if(fileNameArray[j].equalsIgnoreCase("4")){
						for(int k = 0; k < class4.size(); k++){
							out.append("2 " + class4.get(k).substring(2, class4.get(k).length()));						
						}	
					} else if(fileNameArray[j].equalsIgnoreCase("5")){
						for(int k = 0; k < class5.size(); k++){
							out.append("2 " + class5.get(k).substring(2, class5.get(k).length()));						
						}	
					}
				}
			} else if(fileNameArray.length == 2){
				for(int j = 0; j < fileNameArray.length; j++){
					if(fileNameArray[j].equalsIgnoreCase("1")){
						for(int k = 0; k < class1.size(); k++){
							out.append(class1.get(k));					
						}	
					} else if(fileNameArray[j].equalsIgnoreCase("2")){
						for(int k = 0; k < class2.size(); k++){
							out.append(class2.get(k));					
						}	
					} else if(fileNameArray[j].equalsIgnoreCase("3")){
						for(int k = 0; k < class3.size(); k++){
							out.append(class3.get(k));					
						}
					} else if(fileNameArray[j].equalsIgnoreCase("4")){
						for(int k = 0; k < class4.size(); k++){
							out.append(class4.get(k));					
						}	
					} else if(fileNameArray[j].equalsIgnoreCase("5")){
						for(int k = 0; k < class5.size(); k++){
							out.append(class5.get(k));					
						}	
					}
				}
			}

			out.flush();
			out.close();
		}

	}

	static void writeFile(String fileName, String writeString) {
		try {
			File fileDir = new File(fileName);
			BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(fileDir), "UTF8"));

			out.append(writeString);

			out.flush();
			out.close();
		} catch (UnsupportedEncodingException e) {
			System.out.println(e.getMessage());
		} catch (IOException e) {
			System.out.println(e.getMessage());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
}
